package com.base;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.soar.context.AbstractLoginUser;
import com.soar.context.AuthElement;
import com.soar.context.Environment;
import com.soar.jdbc.dbhelper.DbParameter;
import com.soar.jdbc.dbhelper.DbTemplates;
import com.soar.jdbc.dbhelper.SameTypes;
import com.soar.jdbc.dbhelper.SortTypes;
import com.soar.jdbc.dbhelper.SqlTypes;
import com.soar.jdbc.dbhelper.WhereTypes;
import com.soar.senv.SoarProperties;
import com.soar.util.StringUtils;
import com.soar.util.tree.TreeBuilder;
import com.soar.util.tree.TreeNode;

/**
 * 
 * @author zhangaoxiang
 * @version $Revision$ - $Author$ at $Date$
 * @description com.base.LoginUser
 */
public class LoginUser extends AbstractLoginUser
{
	// 登录用户权限代码
	public String UserRoleCode;
	//登录用户组织机构代码
	public String UserOrgCode;

	public String getUserRoleCode()
	{
		return UserRoleCode;
	}
	public void setUserRoleCode(String userRoleCode)
	{
		UserRoleCode = userRoleCode;
	}
	public String getUserOrgCode()
	{
		return UserOrgCode;
	}
	public void setUserOrgCode(String userOrgCode)
	{
		UserOrgCode = userOrgCode;
	}
	private StringBuilder spaceAuth = new StringBuilder();
	private StringBuilder moduleAuth = new StringBuilder();
	private StringBuilder menuAuth = new StringBuilder();
	private StringBuilder activeAuth = new StringBuilder();
	private List<String> pointAuth = new ArrayList<String>();
	
	@Override
	public void loadAuth(Environment env)
	{
		try
		{
			DbParameter param = new DbParameter("APP_MENU t1, USER_ROLE t2, APP_ROLE_AUTH t3");
			param.setSelectFields("distinct T1.ID,T1.PID,T1.MU_NAME,T1.MU_ORDER,T1.MU_ICON,T1.MU_URL,T1.MU_DISPLAY,T1.MU_LEVEL,T1.OPEN_MODE,T1.MU_MODE,T1.MU_POSTION,T1.REQUEST_METHOD,T1.MODE_CODE");
			param.setWhereParams("T3.ROLE_ID", "T2.ROLE_ID", SqlTypes.CUSTOM, WhereTypes.AND, SameTypes.YES);
			param.setWhereParams("T3.MENU_ID", "T1.ID", SqlTypes.CUSTOM, WhereTypes.AND, SameTypes.YES);
			param.setWhereParams("T2.USER_ID", this.getUserId(), SqlTypes.CHAR, WhereTypes.AND, SameTypes.YES);
			param.setWhereParams("T1.MU_DISPLAY", "9", SqlTypes.CHAR, WhereTypes.AND, SameTypes.NO);
			param.setWhereParams("T1.MU_PLATFORM", "1", SqlTypes.CHAR, WhereTypes.AND, SameTypes.YES);
			param.setOrderParams("T1.MU_ORDER", SortTypes.ASC);
			List<AuthElement> data = new ArrayList<AuthElement>();
			List<HashMap<String, String>> list = DbTemplates.select(env.getConnection(), param);
			for (HashMap<String, String> mm : list)
			{
				AuthElement element = new AuthElement();
				element.setId(mm.get("ID"));
				element.setParentId(mm.get("PID"));
				element.setName(mm.get("MU_NAME"));
				element.setOrder(Integer.valueOf(mm.get("MU_ORDER")));
				element.setIcon(mm.get("MU_ICON"));
				element.setLink(mm.get("MU_URL"));
				element.setDisplay(mm.get("MU_DISPLAY").equals("1") ? true : false);
				element.setLevel(mm.get("MU_LEVEL"));
				element.setOpenMode(mm.get("OPEN_MODE"));
				element.setMode(mm.get("MU_MODE"));
				element.setPostion(mm.get("MU_POSTION"));
				element.setRequestMethod(mm.get("REQUEST_METHOD"));
				element.setCode(mm.get("MODE_CODE"));
				data.add(element);
			}
			TreeBuilder<AuthElement> tree = new TreeBuilder<AuthElement>(data);
			tree.setRoot("000000000000000000");
			List<AuthElement> auth = tree.builder();
			this.buildMenu(auth,env);
		}
		catch (SQLException e)
		{
		}
	}
	@Override
	public void reloadAuth(Environment env)
	{
		this.loadAuth(env);
	}
	@Override
	public void buildMenu(List<AuthElement> auth,Environment env)
	{
		//打开方式
		String openModeSelf = "1" ;//原页面
		String openModeNew = "2" ;//新页面
		String openModeDiy = "3" ;//自定义
		
		//菜单模式
		String muModeNormal = "0" ;//常规
		String muModeHref = "1" ;//链接
		String muModeDiy = "2" ;//自定义
		
		//仅处理一下组合： 10、11、21、32、12
		
		this.spaceAuth.delete(0, this.spaceAuth.length());
		this.moduleAuth.delete(0, this.moduleAuth.length());
		this.menuAuth.delete(0, this.menuAuth.length());
		this.activeAuth.delete(0, this.activeAuth.length());
		this.pointAuth.clear();
		
		String activeId = null;
		
		for (AuthElement ae : auth)
		{
			String postion = ae.getPostion() ;
			
			String openModeAe = ae.getOpenMode() ;
			String muModeAe = ae.getMode() ;
			
			//主菜单
			if("1".equals(postion))
			{
				this.spaceAuth.append("<li class=\"");
				if(ae.isDisplay()&&activeId==null)
				{
					this.spaceAuth.append("active");
					if(activeId==null)
					{
						activeId = ae.getId();
					}
				}
				this.spaceAuth.append("\" data-x=\"menu\" data-target=\"").append(ae.getId()).append("\"") ;
				
				//原页面+常规组合
				if(openModeSelf.equals(openModeAe) && muModeNormal.equals(muModeAe) && StringUtils.isNoEmpty(ae.getLink()))
				{
					this.spaceAuth.append(" onclick=\"X.Menu('").append(SoarProperties.SOAR_WEB_CONTEXTROOT).append(ae.getLink()).append("')\"");
				}
				//原页面+链接
				if(openModeSelf.equals(openModeAe) && muModeHref.equals(muModeAe))
				{
					this.spaceAuth.append(" onclick=\"window.open('").append(ae.getLink()).append("','_self')\"");
				}
				//新页面+链接
				if(openModeNew.equals(openModeAe) && muModeHref.equals(muModeAe))
				{
					this.spaceAuth.append(" onclick=\"window.open('").append(ae.getLink()).append("','_blank')\"");
				}
				//自定义+常规
				if(openModeDiy.equals(openModeAe) && muModeNormal.equals(muModeAe))
				{
					this.spaceAuth.append(" onclick=\"").append(ae.getLink()).append("\"") ;
				}
//				//自定义+自定义(暂不处理)
//				if(openModeDiy.equals(openModeAe) && muModeDiy.equals(muModeAe))
//				{
//					this.spaceAuth.append(ae.getLink()) ;
//				}
				
				this.spaceAuth.append("\">");
				this.spaceAuth.append("<a class=\"").append(ae.getIcon()).append("\" alt=\"").append(ae.getName()).append("\"></a>");
				this.spaceAuth.append("<span class=\"vnav-bar-span\">").append(ae.getName()).append("</span>");
				this.spaceAuth.append("</li>");
				
				for (TreeNode node : ae.getChildNodes())
				{
					AuthElement am = (AuthElement)node;
					
					this.moduleAuth.append("<nav data-x=\"submenu\" data-mv=\"").append(am.getParentId()).append("\"");
					
					if(am.isDisplay()&&am.getParentId().equals(activeId))
					{
						this.moduleAuth.append(" style=\"display: block\"");
					}
					else
					{
						this.moduleAuth.append(" style=\"display: none\"");
					}
					this.moduleAuth.append(">");
					this.moduleAuth.append("<div data-x=\"xmenu\">");
					this.moduleAuth.append("<span class=\"").append(am.getIcon()).append("\" style=\"margin-right: 2px\"></span>");
					this.moduleAuth.append("<span class=\"vnav-item-label\">").append(am.getName()).append("</span>");
					if(am.isDisplay())
					{
						this.moduleAuth.append("<span class=\"vnav-item-angle-down\"></span>");
					}
					else
					{
						this.moduleAuth.append("<span class=\"vnav-item-angle-right\"></span>");
					}
					this.moduleAuth.append("</div>");
					
					this.moduleAuth.append("<ul style=\"display: block;\">");
					for(TreeNode mnode:am.getChildNodes())
					{
						AuthElement mm = (AuthElement)mnode;
						
						String openMode = mm.getOpenMode() ;
						String muMode = mm.getMode() ;
						
						//原页面+常规组合
						if(openModeSelf.equals(openMode) && muModeNormal.equals(muMode))
						{
							this.moduleAuth.append("<li data-x=\"imenu\" onclick=\"X.Menu('").append(SoarProperties.SOAR_WEB_CONTEXTROOT).append(mm.getLink()).append("')\">").append("<i class=\"").append(mm.getIcon()).append("\" style=\"margin-right: 2px\"></i>").append(mm.getName()).append("</li>");
						}
						//原页面+链接
						if(openModeSelf.equals(openMode) && muModeHref.equals(muMode))
						{
							this.moduleAuth.append("<li data-x=\"imenu\" onclick=\"window.open('").append(mm.getLink()).append("','_self')\">").append("<i class=\"").append(mm.getIcon()).append("\" style=\"margin-right: 2px\"></i>").append(mm.getName()).append("</li>");
						}
						//新页面+链接
						if(openModeNew.equals(openMode) && muModeHref.equals(muMode))
						{
							this.moduleAuth.append("<li data-x=\"imenu\" onclick=\"window.open('").append(mm.getLink()).append("','_blank')\">").append("<i class=\"").append(mm.getIcon()).append("\" style=\"margin-right: 2px\"></i>").append(mm.getName()).append("</li>");
						}
						//自定义+常规
						if(openModeDiy.equals(openMode) && muModeNormal.equals(muMode))
						{
							this.moduleAuth.append("<li data-x=\"imenu\" onclick=").append(mm.getLink()).append(">").append("<i class=\"").append(mm.getIcon()).append("\" style=\"margin-right: 2px\"></i>").append(mm.getName()).append("</li>") ;
						}
						//自定义+自定义
						if(openModeDiy.equals(openMode) && muModeDiy.equals(muMode))
						{
							this.moduleAuth.append(mm.getLink()) ;
						}
						
					}
					this.moduleAuth.append("</ul>");
					this.moduleAuth.append("</nav>");
				}
			}
			//右侧顶部菜单
			if("2".equals(postion))
			{
				//原页面+常规组合
				if(openModeSelf.equals(openModeAe) && muModeNormal.equals(muModeAe))
				{
					this.activeAuth.append("<li onclick=\"X.Menu('").append(SoarProperties.SOAR_WEB_CONTEXTROOT).append(ae.getLink()).append("')\"><a class=\"").append(ae.getIcon()).append("\" alt=\"").append(ae.getName()).append("\"></a></li>") ;
				}
				//原页面+链接
				if(openModeSelf.equals(openModeAe) && muModeHref.equals(muModeAe))
				{
					this.activeAuth.append("<li onclick=\"window.open('").append(ae.getLink()).append("','_self')\"><a class=\"").append(ae.getIcon()).append("\" alt=\"").append(ae.getName()).append("\"></a></li>") ;
				}
				//新页面+链接
				if(openModeNew.equals(openModeAe) && muModeHref.equals(muModeAe))
				{
					this.activeAuth.append("<li onclick=\"window.open('").append(ae.getLink()).append("','_blank')\"><a class=\"").append(ae.getIcon()).append("\" alt=\"").append(ae.getName()).append("\"></a></li>") ;
				}
				//自定义+常规
				if(openModeDiy.equals(openModeAe) && muModeNormal.equals(muModeAe))
				{
					this.activeAuth.append("<li onclick=\"").append(ae.getLink()).append("\"><a class=\"").append(ae.getIcon()).append("\" alt=\"").append(ae.getName()).append("\"></a></li>") ;
				}
				//自定义+自定义
				if(openModeDiy.equals(openModeAe) && muModeDiy.equals(muModeAe))
				{
					this.activeAuth.append(ae.getLink()) ;
				}
			}
		}
		
		env.getSession().setAttribute(SoarProperties.SOAR_BM_WP_SESSION_ID, this.spaceAuth.toString());
		env.getSession().setAttribute(SoarProperties.SOAR_BM_MENU_SESSION_ID, this.moduleAuth.toString());
		env.getSession().setAttribute(SoarProperties.SOAR_BM_SHORTCUT_MENU_SESSION_ID, this.activeAuth.toString());
	}
}
